package com.zp.drug.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.bo.DrugImportMasterBo;
import com.zp.drug.entity.vo.DrugImportMasterVo;
import com.zp.drug.service.IDrugImportMasterService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 药品入库主表控制器
 * 前端访问路由地址为:/drug/drugImportMaster
 *
 * @author zhang peng
 * @ date 2023-06-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugImportMaster")
public class DrugImportMasterController extends BaseController {

    private final IDrugImportMasterService iDrugImportMasterService;

    /**
     * 查询药品入库主表列表 (分页)
     */
    @SaCheckPermission("drug:drugImportMaster:list")
    @GetMapping("/selectPage")
    public TableDataInfo<DrugImportMasterVo> selectPage(DrugImportMasterBo bo, PageQuery pageQuery) {
        return iDrugImportMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询药品入库主表列表 (不分页)
     */
    @SaCheckPermission("drug:drugImportMaster:list")
    @GetMapping("/list")
    public R<List<DrugImportMasterVo>> list(DrugImportMasterBo bo) {
        List<DrugImportMasterVo> list = iDrugImportMasterService.queryList(bo);
        return R.ok(list);
    }

    /**
     * @return
     * @Author zhangpeng
     * @Description // 入库药品检索
     * @ date 17:54 2023/6/23
     * @Param * @param null
     **/
    @GetMapping("/getAutoDrugPriceInfo")
    public R<List<DrugImportMasterVo>> getAutoDrugPriceInfo(DrugImportMasterBo bo) {
        List<DrugImportMasterVo> list = iDrugImportMasterService.getAutoDrugPriceInfo(bo);
        return R.ok(list);
    }

    /**
     * 导出药品入库主表列表
     */
    @SaCheckPermission("drug:drugImportMaster:export")
    @Log(title = "药品入库主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DrugImportMasterBo bo, HttpServletResponse response) {
        List<DrugImportMasterVo> list = iDrugImportMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品入库主表", DrugImportMasterVo.class, response);
    }

    /**
     * 获取药品入库主表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("drug:drugImportMaster:list")
    @GetMapping("/{id}")
    public R<DrugImportMasterVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iDrugImportMasterService.queryById(id));
    }

    /**
     * 确认入库
     *
     * @param id 主键
     */
    @SaCheckPermission("drug:drugImportMaster:confirmImport")
    @GetMapping("/confirmImport/{exportId}")
    public R<Void> confirmImport(@NotNull(message = "主键不能为空") @PathVariable String exportId) {
        return toAjax(iDrugImportMasterService.confirmImport(exportId));
    }

    /**
     * 新增药品入库主表
     */
    @SaCheckPermission("drug:drugImportMaster:add")
    @Log(title = "药品入库主表", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DrugImportMasterBo bo) {
        return toAjax(StringUtils.isNotBlank(iDrugImportMasterService.insertByBo(bo).getId()) ? 1 : 0);
    }

    /**
     * 修改药品入库主表
     */
    @SaCheckPermission("drug:drugImportMaster:edit")
    @Log(title = "药品入库主表", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DrugImportMasterBo bo) {
        return toAjax(iDrugImportMasterService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品入库主表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("drug:drugImportMaster:remove")
    @Log(title = "药品入库主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iDrugImportMasterService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
